package com.hm.base.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hm.base.auto.su.BeanUtil;
import com.hm.base.domain.Menu;
import com.hm.base.mapper.MenuMapper;
import com.hm.base.vo.MenuVo;
import com.hm.common.def.DataStatusDef;
import com.hm.common.def.WhetherDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.DateUtil;

/**
 * @author shishun.wang
 * @date 下午9:49:09 2017年8月17日
 * @version 1.0
 * @describe
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	@Override
	public List<MenuVo> queryList(Long parentId) {
		Menu menu = new Menu();
		menu.setStatus(DataStatusDef.ENABLE.name());
		if (CommonUtil.isNotEmpty(parentId)) {
			menu.setParentId(parentId);
		} else {
			menu.setParentId(0l);
		}

		List<MenuVo> vos = new ArrayList<MenuVo>();
		menuMapper.select(menu).forEach(item -> {
			MenuVo vo = new MenuVo();
			BeanUtil.copyProperties(item, vo);
			vo.setSystemCfg(WhetherDef.trance(item.getSystemDefault()) == WhetherDef.YES);
			vo.setCreateTime(DateUtil.data2long(item.getCreateTime()));
			if (CommonUtil.isNotEmpty(vo.getParentId())) {
				Menu parentMenu = menuMapper.selectByPrimaryKey(vo.getParentId());
				if (CommonUtil.isNotEmpty(parentMenu)) {
					vo.setParentName(parentMenu.getName());
				}
			}

			vos.add(vo);
		});

		return vos;
	}

	@Override
	public MenuVo getMenu(Long menuId) {
		if (CommonUtil.isEmpty(menuId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}
		Menu menu = menuMapper.selectByPrimaryKey(menuId);
		if (CommonUtil.isEmpty(menu)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		MenuVo vo = new MenuVo();
		BeanUtil.copyProperties(menu, vo);
		if (CommonUtil.isNotEmpty(vo.getParentId())) {
			Menu parentMenu = menuMapper.selectByPrimaryKey(vo.getParentId());
			if (CommonUtil.isNotEmpty(parentMenu)) {
				vo.setParentName(parentMenu.getName());
			}
		}

		return vo;
	}

	@Override
	public List<MenuVo> getSystemMenus(long parentId) {
		Menu menu = new Menu();
		menu.setStatus(DataStatusDef.ENABLE.name());
		menu.setParentId(parentId);
		List<MenuVo> vos = new ArrayList<MenuVo>();
		menuMapper.select(menu).forEach(item -> {
			MenuVo vo = new MenuVo();
			BeanUtil.copyProperties(item, vo);
			vo.setCreateTime(DateUtil.data2long(item.getCreateTime()));
			vo.setSystemCfg(WhetherDef.trance(item.getSystemDefault()) == WhetherDef.YES);
			vo.setChilds(getSystemMenus(item.getId()));
			vos.add(vo);
		});
		return vos;
	}

	@Override
	public void addMenu(MenuVo menuVo) {
		if (CommonUtil.isAnyEmpty(menuVo.getName(), menuVo.getCreateUser())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}
		Menu menu = new Menu();
		BeanUtil.copyProperties(menuVo, menu);
		menu.setId(null);
		menu.setSystemDefault(WhetherDef.NO.name());
		menu.setStatus(DataStatusDef.ENABLE.name());
		menu.setCreateTime(new Date());

		menuMapper.insert(menu);
	}

	@Override
	public void updateMenu(MenuVo menuVo) {
		if (CommonUtil.isAnyEmpty(menuVo.getId(), menuVo.getName())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}
		Menu menu = menuMapper.selectByPrimaryKey(menuVo.getId());
		if (CommonUtil.isEmpty(menu)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		menu.setName(menuVo.getName());
		menu.setNote(menuVo.getNote());
		menu.setIco(menuVo.getIco());
		menu.setSort(menuVo.getSort());

		menuMapper.updateByPrimaryKey(menu);
	}

	@Override
	public void deleteMenu(List<Long> menuIds) {
		if (CommonUtil.isEmpty(menuIds)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		menuIds.parallelStream().forEach(menuId -> {
			Menu menu = menuMapper.selectByPrimaryKey(menuId);
			if (CommonUtil.isEmpty(menu)) {
				return;
			}
			if (WhetherDef.trance(menu.getSystemDefault()) == WhetherDef.NO) {
				menu.setStatus(DataStatusDef.DISABLE.name());
				menuMapper.updateByPrimaryKey(menu);
			}
		});
	}
}
